#pragma once

#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <pthread.h>
#include <string.h>
#include <functional>
#include <memory>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#include "InetAddr.hpp"
#include "Log.hpp"

// 模板方法模式
namespace socket_ns
{
    class Socket;
    const static int gbacklog = 8;
    using socket_sptr = std::shared_ptr<Socket>;

    enum
    {
        SOCKET_ERROR = 1,
        BIND_ERROR,
        USAGE_ERROR,
        LISTEN_ERROR,
    };

    // std::unique_ptr<Socket> listensock=std::make_unique<TcpSocket>();
    // listensock->BuildListenSocket();
    // std::unique_ptr<Socket> clientsock=std::make_unique<TcpSocket>();
    // clientsock->BuildClientSocket();


    class Socket
    {
    public:
        virtual void CreatSocketOrDie() = 0;
        virtual void BindSocketOrDie(InetAddr &addr) = 0;
        virtual void ListenSocketOrDie() = 0;
        virtual socket_sptr Accepter(InetAddr *addr) = 0;
        virtual bool Connetcor(InetAddr &addr) = 0;//const?

    public:
        void BuildListenSocket(InetAddr &addr)
        {
            CreatSocketOrDie();
            BindSocketOrDie(addr);
            ListenSocketOrDie();
        }
        bool BuildClientSocket(InetAddr &addr)//const?
        {
            CreatSocketOrDie();
            return Connetcor(addr);
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int fd=-1) : _sockfd(fd)
        {
        }
        void CreatSocketOrDie() override
        {
            // 1.创建流式套接字
            //_sockfd = socket(AF_INET, SOCK_DGRAM, 0);udp
            _sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_sockfd < 0)
            {
                LOG(FATAL, "sockfd error\n");
                exit(SOCKET_ERROR);
            }
            LOG(DEBUG, "socket create success,socket:%d\n", _sockfd);
        }
        void BindSocketOrDie(InetAddr &addr) override
        {
            // 2.bind
            struct sockaddr_in local;
            memset(&local, 0, sizeof(local));
            local.sin_family = AF_INET;
            local.sin_port = htons(addr.Port());
            local.sin_addr.s_addr = inet_addr(addr.Ip().c_str());

            int n = bind(_sockfd, (struct sockaddr *)&local, sizeof(local));
            if (n < 0)
            {
                LOG(FATAL, "bind error\n");
                exit(BIND_ERROR);
            }
        }
        void ListenSocketOrDie() override
        {
            int n = listen(_sockfd, gbacklog);
            if (n < 0)
            {
                LOG(FATAL, "listen error\n");
                exit(LISTEN_ERROR);
            }
        }
        socket_sptr Accepter(InetAddr *addr) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sockfd = accept(_sockfd, (struct sockaddr *)&peer, &len);
            if (sockfd < 0)
            {
                LOG(WARNING, "accept error\n");
                return nullptr;
            }
            *addr = peer;
            socket_sptr sock = std::make_shared<TcpSocket>(sockfd);
            return sock;
        }
        bool Connetcor(InetAddr &addr) override
        {
            struct sockaddr_in server;
            // 构建目标主体的socket信息
            memset(&server, 0, sizeof(server));
            server.sin_family = AF_INET;
            server.sin_port = htons(addr.Port());
            server.sin_addr.s_addr = inet_addr(addr.Ip().c_str());

            int n = connect(_sockfd, (struct sockaddr *)&server, sizeof(server));
            if (n < 0)
            {
                std::cerr << "connect error"<<std::endl;
                return false;
            }
            return true;
        }

    private:
        int _sockfd;
    };
} // namespace socket_ns